<template>
	<view class="top">
		<view class="kefu">
			在线客服
		</view>
		<view class="kefu">
			一键呼叫
		</view>
	</view>
	<view class="content">
		<view class="nav">
			<!-- 选项卡水平方向滑动，scroll-with-animation是滑动到下一个选项时，有一个延时效果 -->
			<scroll-view class="tab-scroll" scroll-x="true" scroll-with-animation :scroll-left="scrollLeft">
				<view class="tab-scroll_box">
					<!-- 选项卡类别列表 -->
					<view class="tab-scroll_item" v-for=" (item,index) in category" :key="index"
						:class="{'active':isActive==index}" @click="chenked(index)">
						{{item.name}}
					</view>
				</view>
			</scroll-view>
		</view>
		<!-- 选项卡内容轮播滑动显示，current为当前第几个swiper子项 -->
		<swiper @change="change" :current="isActive" class="swiper-content" :style="fullHeight">
			<swiper-item class="swiperitem-content" v-for="(tab, index) in category" :key="index">
				<scroll-view scroll-y style="height: 100%;">
					<template v-if="tabContents[index] && tabContents[index].length">
						<view v-for="(item, index) in tabContents[index]" class="nav_item" @click="onClickItem(item)">
							{{ index + 1 }}、{{ item.title }}
						</view>
					</template>
					<empty v-else :show="false"></empty>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
	<!-- 普通弹窗 -->
	<uni-popup ref="popup" type="bottom" background-color="#fff">
		<view class="popup-content">
			<view class="popup-content__item">
				<image class="icon" src='http://jiaoyukehu.oss-cn-beijing.aliyuncs.com/static/question/ask.png' />
				<text class="popup-content__title">{{ currPopupData?.title }}</text>
			</view>
			<view class="popup-content__item">
				<image class="icon" src="http://jiaoyukehu.oss-cn-beijing.aliyuncs.com/static/question/answer.png" />
				<text class="popup-content__title-content">{{ currPopupData?.content }}</text>
			</view>
		</view>
	</uni-popup>
</template>
<script>
	import uniPopup from "@/uni_modules/uni-popup/components/uni-popup/uni-popup.vue";
	import empty from "@/components/empty/index.vue";
	export default {
		components: {
			empty
		},
		watch: {
			// swiper与上面选项卡联动
			currentindex(newval) {
				this.isActive = newval;
				this.scrollLeft = 0;
				// 滑动swiper后，每个选项距离其父元素最左侧的距离
				for (let i = 0; i < newval - 1; i++) {
					this.scrollLeft += this.category[i].width
				};
			},

		},
		data() {
			return {
				isActive: 0,
				index: 0,
				currentindex: 0,
				tabContents: [],
				currPopupData: null,
				category: [],
				contentScrollW: 0, // 导航区宽度
				scrollLeft: 0, // 横向滚动条位置
				fullHeight: "",
			}
		},
		mounted() {
			var that = this;
			//获取手机屏幕的高度，让其等于swiper的高度，从而使屏幕充满
			uni.getSystemInfo({
				success: function(res) {
					that.fullHeight = "height:" + res.windowHeight + "px";
				}
			});
			// 获取标题区域宽度，和每个子元素节点的宽度
			this.getScrollW();
			this.getTypeList();
		},
		methods: {
			// 获取标题区域宽度，和每个子元素节点的宽度以及元素距离左边栏的距离
			getScrollW() {
				const query = uni.createSelectorQuery().in(this);
				query.select('.tab-scroll').boundingClientRect(data => {
					// 拿到 scroll-view 组件宽度
					this.contentScrollW = data.width
				}).exec();
				query.selectAll('.tab-scroll_item').boundingClientRect(data => {
					let dataLen = data.length;
					for (let i = 0; i < dataLen; i++) {
						//  scroll-view 子元素组件距离左边栏的距离
						this.category[i].left = data[i].left;
						//  scroll-view 子元素组件宽度
						this.category[i].width = data[i].width
					}
				}).exec()
			},
			// 当前点击子元素靠左留一个选项展示，子元素宽度不相同也可实现
			chenked(index) {
				this.isActive = index;
				this.scrollLeft = 0;
				for (let i = 0; i < index - 1; i++) {
					this.scrollLeft += this.category[i].width
				};
			},
			// swiper滑动时，获取其索引，也就是第几个
			change(e) {
				const {
					current
				} = e.detail;
				this.currentindex = current;
			},
			getTypeList() {
				let url = this.$API.getContentTypeList;
				this.$UNIHTTP.getData(url, {}).then((res) => {
					if (res.code == 1) {
						this.category = res.data || [];
						this.getAllTabContent(res.data);
					}
				})
			},
			getAllTabContent(tabs) {
				const url = this.$API.getContentList;
				tabs.forEach(item => {
					this.$UNIHTTP.getData(url, {
						type: item.id
					}).then((res) => {
						if (res.code == 1) {
							this.tabContents.push(res.data || []);
							console.log(this.tabContents, "999999999999")
						}
					})
				});
			},
			onClickItem(item) {
				this.currPopupData = item;
				this.$refs.popup.open('bottom');
			}
		}
	}
</script>
<style lang="scss">
	@import url("question.css");

	page {
		height: 100vh;
		overflow: hidden;
		width: 100%;
		// display: flex;
		background-color: #FFFFFF;
	}

	.content {
		// display: flex;
		flex-direction: column;
		width: 100%;
		flex: 1;

		.nav {
			border-top: 1rpx solid #f2f2f2;
			background-color: #F5F7FA;
			position: fixed;
			z-index: 99;
			width: 100%;
			align-items: center;
			height: 100rpx;

			.tab-scroll {
				flex: 1;
				overflow: hidden;
				box-sizing: border-box;
				padding-left: 30rpx;
				padding-right: 30rpx;

				.tab-scroll_box {
					display: flex;
					align-items: center;
					flex-wrap: nowrap;
					box-sizing: border-box;

					.tab-scroll_item {
						line-height: 60rpx;
						margin-right: 35rpx;
						flex-shrink: 0;
						padding-bottom: 10px;
						display: flex;
						justify-content: center;
						font-size: 16px;
						padding-top: 10px;
					}
				}
			}
		}

		.swiper-content {
			padding-top: 120rpx;
			flex: 1;

			.swiperitem-content {
				background-color: #F5F7FA;

				.nav_item {
					background-color: white;
					padding: 20rpx 40rpx 40rpx 40rpx;

					&:not(:last-of-type) {
						border-bottom: 1px solid #dcdcdc;
					}
				}
			}
		}
	}

	.active {
		position: relative;
		color: green;
		font-weight: 600;
	}

	.active::after {
		content: "";
		position: absolute;
		width: 130rpx;
		height: 4rpx;
		background-color: green;
		left: 0px;
		right: 0px;
		bottom: 0px;
		margin: auto;
	}

	.popup-content {
		min-height: 300rpx;
		padding: 30rpx;

		&__item {
			display: flex;
			algin-item: center;
			margin-bottom: 15rpx;
		}

		&__title {
			font-size: 30rpx;
			font-weight: 600;
		}

		&__title-content {
			font-size: 26rpx;
			color: #666666;
		}

		.icon {
			width: 50rpx;
			height: 50rpx;
			margin-right: 10rpx
		}
	}

	/* 隐藏滚动条，但依旧具备可以滚动的功能 */
	.uni-scroll-view::-webkit-scrollbar {
		/* 样式属性 */
	}
</style>